<?php

class PageBackend extends Page {

    private function __construct() {
        
        $this->addScript( URL_BASE_JAVASCRIPT . '/libraries/jquery/jquery-1.4.2.js' );
        $this->addStyle( URL_BASE_CSS . '/main.css' );
    }

    public static function getInstance() {
        if (!parent::$instance instanceof PageBackend) {
            parent::$instance = new PageBackend( );
        }

        return parent::$instance;
    }

    private function buildBody() {
        $xtpl = new XTemplate(Utility::getFileContent(DIR_TEMPLATES_WEBPAGE . DS . 'layout.tpl.html'));
        $xtpl->assign('content', $this->content);
        $xtpl->assign('header', $this->getHeader());
        $xtpl->assign('footer', $this->getFooter());
        $xtpl->assign('menu', $this->getMenu());
        $xtpl->parse('main');
        $body = $xtpl->out('main');
        $body = $this->renderBody($body);
        return $body;
    }

    private function renderBody($template) {
        return $template;
    }

    public function getBody() {
        $body = $this->buildBody();
        $body = $this->renderBody($body);
        return $body;
    }

    private function getMenu() {
        return $this->menu;
    }

    private function getHeader() {

        $xtpl = new XTemplate(Utility::getFileContent(DIR_TEMPLATES_WEBPAGE . DS . '/header.tpl.html'));
        $xtpl->parse('content');
        return $xtpl->out('content');
    }

    private function getFooter() {
        $xtpl = new XTemplate(Utility::getFileContent(DIR_TEMPLATES_WEBPAGE . DS . '/footer.tpl.html'));
        $xtpl->parse('content');
        return $xtpl->out('content');
    }

    public function setMenu($menu) {
        $this->menu = $menu;
    }

    public function setHeader($header) {
        $this->header = $header;
    }

    public function setFooter($footer) {
        $this->footer = $footer;
    }

}

?>